package cn.com.dhc.zzz.back.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.base.UserInfo;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.format.ZZZFormat;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.mail.MailUtil;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.path.BasePath;
import cn.bbs.ba.util.CommonUntil;
import cn.bbs.ba.util.DtoUtil;
import cn.com.dhc.zzz.form.UserInfoForm;
import cn.com.zzz.dao.PPUserInfoDao;

/**
 * 添加用户
 */
public class AddUserAction extends BAAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 判断是否登录
		super.execute(mapping, form, request, response);
		if(super.userInfo == null) {
			return mapping.findForward("comError");
		}
		UserInfo userInfo = new UserInfo();
		DtoUtil.formToDto(form, userInfo);
		trac("追加用户开始" + userInfo.getUserName());
		String strPwd = userInfo.getPASSPORT();
		if (StringUtils.isEmpty(strPwd)) {
			return mapping.findForward("comError");
		}
		PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
		UserInfo maxUserInfo = userInfoDao.getMaxUser();
		String userNO = CommonUntil.add(maxUserInfo.getUserNo(), "1");
		// 用户编号
		userInfo.setUserNo(userNO);
		// 用户权限
		String initUright = BasePath.getPath("initUright");
		userInfo.setUright(initUright);
		// 密码加密
		userInfo.setPASSPORT(BAMima.encry(userInfo.getAlias() + userInfo.getPASSPORT()));
		// 状态（ 0：正常；1：暂时冻结；2：冻结 3 销户 4:没有验证密码 9：已删除）
		userInfo.setSTT("4");
		// 注册IP地址
		userInfo.setRemarks(super.getRemortIp(request));
		// 有效开始日期
		userInfo.setOpenDate(ZZZFormat.getDifferentDate(0, "yyyyMMddHHmmss"));
		// 有效结束日期
//		userInfo.setEndDate(ZZZFormat.getDifferentDate(CommonUntil.objToint(ZPaiConstant.getCodeValue("PK001", userInfo.getKbn())), "yyyyMMddHHmmss"));
		userInfo.setEndDate("99999999999999");
		userInfo.setIpAdd(super.getRemortIp(request));
		userInfo.setOpenUserId(super.userInfo.getUserNo());
		userInfo.setAlias(userInfo.getUserName());
		// 密码登录
		userInfo.setLOGONTYPE("0");
		String strValue = ZPaiConstant.getCodeValue("PK004", "000");
		strValue = strValue.replaceAll("#1", userInfo.getUserName());
		strValue = strValue.replaceAll("#2", ((UserInfoForm)form).getShowPASSPORT());
		// 发送MAIL
		MailUtil.sendMail(ZPaiConstant.disPlay("扑克城开户通知"), strValue, userInfo.getEmail());
		int insertCnt = userInfoDao.insertUserInfo(userInfo);
		
		if (insertCnt == 1) {
			// 建立登陆SESSION
			request.setAttribute("showValue", ZPaiConstant.disPlay("添加成功，请确认EmaiL"));
			//先到手机验证页面
			//return mapping.findForward("userCenter-13");
			// 会到主页 手机验证取消
			return mapping.findForward("success");
		}
		else {
			
			// 用户已经存在
			return mapping.findForward("success");
		}
	}

}
